package com.project.movie.controllers;

import com.project.movie.entities.Actor;
import com.project.movie.entities.Director;
import com.project.movie.entities.Movies;
import com.project.movie.entities.Roles;
import com.project.movie.services.ActorService;
import com.project.movie.services.DirectorService;
import com.project.movie.services.MovieService;
import com.project.movie.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("movies")
public class MovieControllers {

    @Autowired
    MovieService movieService;
    @Autowired
    DirectorService directorService;
    @Autowired
    RoleService roleService;
    @Autowired
    ActorService actorService;

    @GetMapping
    public  String displayStudents(Model model){
        List<Movies> Movies = movieService.getAll();
        model.addAttribute("movies",Movies);
        return "movies";
    }


    @GetMapping("/new")
    public String displayMovieForm(Model model){

        Movies aMovie=new Movies();
        List<Roles> roles=roleService.getAll();
        List<Actor> actors=actorService.getAll();
        List<Director> directors=directorService.getAll();
        model.addAttribute("movies",aMovie);
        model.addAttribute("role",roles);
        model.addAttribute("actor",actors);
        model.addAttribute("director",directors);
        return "new-movie";
    }

    @PostMapping("/save")
    public String creatMovie(Model model, @RequestParam (value = "roles",required=false) List<Long> roles,@RequestParam (value = "actors",required=false) List<Long> actors,Director director, Movies movies ){
        movieService.save(movies);

        return "redirect:/movies";
    }

}
